package com.suzhe.ymall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.product.pojo.Attribute;
import com.suzhe.ymall.product.param.AttributeParam;
import com.suzhe.ymall.product.service.IAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性表 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-07-09
 */
@RestController
@RequestMapping("/product/attribute")
@Slf4j
public class AttributeController {

    @Autowired
    IAttributeService iAttributeService;

    @GetMapping("/list")
    @LoginAuth("productAttributeView")
    public ApiResEntity list(AttributeParam queryParam){
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",queryParam.getCategoryId());
        if (StringUtils.isNotBlank(queryParam.getName())){
            queryWrapper.like("name",queryParam.getName());
        }
        queryWrapper.eq("enable",queryParam.isEnable());
        List<Attribute> list = iAttributeService.list(queryWrapper);
        return ApiResEntity.ok(list);
    }


    @PostMapping("/save")
    @LoginAuth("productAttributeSave")
    public ApiResEntity save(@RequestBody Attribute attribute){
        log.debug("save attribute:{}",attribute);
        iAttributeService.saveOrUpdate(attribute);
        return ApiResEntity.ok();
    }

    @PutMapping("/update")
    @LoginAuth("productAttributeUpdate")
    public ApiResEntity edit(@RequestBody Attribute attribute){
        log.debug("update attribute:{}",attribute);
        iAttributeService.saveOrUpdate(attribute);
        return ApiResEntity.ok();
    }

    @DeleteMapping("/delete")
    @LoginAuth("productAttributeDelete")
    public ApiResEntity delete(@RequestBody Attribute attribute){
        log.debug("delete attribute:{}",attribute);
        iAttributeService.removeById(attribute.getId());
        return ApiResEntity.ok();
    }

}
